package starter.game;

import java.util.Date;

import starter.context.App;
import starter.game.netty.GameTcpServer;
import starter.tool.Shutdown;

import com.bowlong.lang.NumEx;
import com.bowlong.lang.ThreadEx;
import com.bowlong.tool.Lib2;
import com.bowlong.util.Log;

/**
 * @Description ：逻辑服务器启动入口
 */
@SuppressWarnings("static-access")
public class GameStartUp extends Lib2 {
	static final Runtime runtime = Runtime.getRuntime();

	public static void main(String[] args) {
		String gameName = App.game.name;
		int tcpPort = App.game.port;
		int httpPort = App.game.httpPort;
		int manaPort = App.game.manaPort;
		int shutdownPort = App.game.shutdownPort;
		String version = App.game.nowVer;
		// 对应网关信息
		String gateHost = App.game.gateHost;
		int gatePort = App.game.gatePort;
		// 日志开关
		// boolean openLog4j = App.openLog4j();
		try {
			long l1 = now();
			String sdate = tFmt(new Date());
			long freeMem1 = runtime.freeMemory();
			// 启动一个关闭监听
			Shutdown shutdown = new Shutdown(shutdownPort);
			shutdown.start();

			// 预加载系统数据
			App.game.loadAll();

			// 启动TCP服务
			GameTcpServer.start(tcpPort);

			long freeMem2 = runtime.freeMemory();
			long totalMemory = runtime.totalMemory();

			String sdate2 = tFmt(new Date());
			long l2 = now();
			StringBuffer sb = new StringBuffer();
			sn(sb, "");
			sn(sb, "/////////////////////////////////////////");
			sn(sb, "// Applicatin   :%s v: %s", gameName, version);
			sn(sb, "// TCP_PORT  on :%s", tcpPort + "");
			sn(sb, "// HTTP_PORT on :%s", httpPort + "");
			sn(sb, "// MANA_PORT on :%s", manaPort + "");
			sn(sb, "// Shutdown  on :%s", shutdownPort + "");
			sn(sb, "// GATE_HOST on :%s", gateHost + "：" + gatePort + "");
			sn(sb, "// used Memory  :%s", NumEx.MB(freeMem1 - freeMem2) + "MB");
			sn(sb, "// free Memory  :%s", NumEx.MB(freeMem2) + "MB");
			sn(sb, "// totalMemory  :%s", NumEx.MB(totalMemory) + "MB");
			sn(sb, "// Start Time   :%s", sdate + "");
			sn(sb, "// start time2  :%s", sdate2 + "");
			sn(sb, "// startup      :%s", (l2 - l1) + " ms");
			sn(sb, "/////////////////////////////////////////");
			Log.info(sb);

			// Log.init(openLog4j);
		} catch (Exception e) {
			try {
				String sdate = tFmt(new Date());
				StringBuffer sb = new StringBuffer();
				sn(sb, "");
				sn(sb, "/////////////////////////////////////////");
				sn(sb, "// Applicatin   :%s", gameName + "");
				sn(sb, "// Applicatin   :%s v:%s", gameName, version);
				sn(sb, "// TCP_PORT   :%s", tcpPort + "");
				sn(sb, "// Shutdown   :%s", shutdownPort + "");
				sn(sb, "// Start Time   :%s", sdate + "");
				sn(sb, "// Exception    :%s", e2s(e) + "");
				sn(sb, "/////////////////////////////////////////");
				Log.error(sb, e);
			} catch (Exception e2) {
				Log.error(e2s(e2));
			}
			ThreadEx.Sleep(1000);
			System.exit(1);
		}
	}
}

// java -server -Xmx4000M -Xms4000M -Xmn2000M -Xss256K -XX:PermSize=500M
// -XX:MaxPermSize=500M -XX:MaxTenuringThreshold=20 -XX:SurvivorRatio=2
// -XX:+UseConcMarkSweepGC -XX:+PrintGCTimeStamps -XX:+PrintGC
// -XX:+PrintGCDetails -Xloggc:logs/gc.log -XX:+HeapDumpOnOutOfMemoryError
// -Dfile.encoding=UTF-8 